home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TeX 1995 July
/
TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO
/
macros
/
latex209
/
contrib
/
eepic
/
grafig.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-08
|
4KB
|
184 lines
/*
* GraFig 0.0 - Generates Numerical Graphs in Fig Code
*
* Author: Micah Beck
* (email: beck@cs.cornell.edu)
* Dept. of Computer Science
* Cornell University
*
* Version 0.0: February 1, 1989
*/
#include <stdio.h>
#include "object.h"
#define FIG_MAGIC "#FIG 1.4"
#define RES 80
#define COORD_SYS 2
#define MAX_DATA 1000
#define MARK_SIZE .05
#define TIC_SIZE .1
#define NUM_OFF_X .5
#define NUM_OFF_Y .25
#define orig_x 1.0
#define orig_y 9.0
#define length_x 5.0
#define length_y 5.0
extern double pow(), floor(), log10();
struct datapt {double x, y} dataset[MAX_DATA], *datasort[MAX_DATA];
main(argc,argv)
int argc;
char **argv;
{
int i, c, data_size, datacmp();
double tic();
double max_x, max_y, min_x, min_y;
double dx, dy;
double tic_x, tic_y, w;
/* read data */
for (data_size=0, c=2; data_size<MAX_DATA-1 && c == 2; data_size++)
c = scanf("%lf %lf", &dataset[data_size].x, &dataset[data_size].y);
if (c == EOF)
--data_size;
else
if (c != 2)
error("bad data format");
else
error("data set truncated");
if (data_size == 0) error("no data!");
/* calculate maxima */
max_x = max_y = dataset[0].x;
for (i=1; i<data_size; i++) {
if (max_x < dataset[i].x) max_x = dataset[i].x;
if (max_y < dataset[i].y) max_y = dataset[i].y;
}
/* calculate scaling factors */
dx = length_x / max_x;
dy = length_y / max_y;
/* print FIG header */
printf("%s\n", FIG_MAGIC);
printf("%d %d\n", RES, COORD_SYS);
/* draw axes */
fig_comment("Graph Axes");
fig_line(orig_x, orig_y, orig_x+length_x, orig_y);
fig_line(orig_x, orig_y, orig_x, orig_y-length_y);
/* plot data points */
fig_comment("Data Points");
for (i=0; i<data_size; i++) {
dataset[i].x = orig_x + (dataset[i].x * dx);
dataset[i].y = orig_y - (dataset[i].y * dy);
fig_line(dataset[i].x-MARK_SIZE, dataset[i].y,
dataset[i].x+MARK_SIZE, dataset[i].y);
fig_line(dataset[i].x, dataset[i].y-MARK_SIZE,
dataset[i].x, dataset[i].y+MARK_SIZE);
}
fig_comment("Data Curve");
qsort((char *)dataset, data_size, sizeof(struct datapt), datacmp);
fig_plot(dataset, data_size);
/* draw tic marks with numbering */
fig_comment("Axis Numbering");
tic_x = tic(max_x);
tic_y = tic(max_y);
for (i=1; (i*tic_x)<=max_x; i++) {
w = orig_x + i*tic_x*dx;
fig_line(w, orig_y, w, orig_y+TIC_SIZE);
fig_number(w, orig_y+NUM_OFF_Y, i*tic_x);
}
for (i=1; (i*tic_y)<=max_y; i++) {
w = orig_y - i*tic_y* dy;
fig_line(orig_x, w, orig_x-TIC_SIZE, w);
fig_number(orig_x-NUM_OFF_X, w, i*tic_y);
}
}
error(s)
char *s;
{
fprintf(stderr, "grafig: %s\n", s);
exit(1);
}
double tic(r)
double r;
{
double t;
t = pow(10.0, floor(log10(r)));
if (2*t>r) return t/10;
else return t;
}
datacmp(a, b)
struct datapt *a, *b;
{
if (a->x < b->x) return -1;
if (a->x > b->x) return 1;
return 0;
}
/*
* Fig Code Generating Routines
*/
fig_comment(str)
char *str;
{
printf("#\n# %s\n#\n", str);
}
fig_line(ax, ay, bx, by)
double ax, ay, bx, by;
{
printf("%d %d %d %d %d %d %d %d %6.3f %d %d\n",
O_POLYLINE, T_POLYLINE,
SOLID_LINE, 1, DEFAULT, DEFAULT, DEFAULT, DEFAULT, 0.0,
0, 0);
printf("%.0f %.0f %.0f %.0f 9999 9999\n",
ax*RES, ay*RES, bx*RES, by*RES);
}
fig_plot(dp, ds)
struct datapt *dp;
int ds;
{
int i;
printf("%d %d %d %d %d %d %d %d %6.3f %d %d\n",
O_POLYLINE, T_POLYLINE,
SOLID_LINE, 1, DEFAULT, DEFAULT, DEFAULT, DEFAULT, 0.0,
0, 0);
for (i=0; i<ds; i++)
printf("%.0f %.0f ", dp[i].x*RES, dp[i].y*RES);
printf("9999 9999\n");
}
fig_number(x, y, num)
double x, y, num;
{
char buf[10];
sprintf(buf, "%6lg", num);
printf("%d %d %d %d %d %d %d %6.3f %d %d %d %.0lf %.0lf %s\01\n",
O_TEXT, T_LEFT_JUSTIFIED,
DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, 0.0, DEFAULT,
16, 8*strlen(buf), x*RES, y*RES, buf);
}